/*
 * Josh Lindoo
 * Login ID : lind6441
 * CS-102, Winter 2013
 * Programming Assignment 3
 * Prog3 class: Controls the program flow of the assignment
 */

import java.io.*;
import java.util.*;

public class Prog3 {
	
	//Instantiate database
	private static Database database = new Database();
	
	/*
	 * Method: main
	 * Purpose: Control the program flow and create linkedlist
	 * 			to store movies and showings
	 * Parameters:
	 * 		String[] args:	used to input the movie/showing file names
	 * Returns: void
	 */
	
	public static void main(String[] args) {
		
		//Scanners
		Scanner console = new Scanner(System.in);	// Keyboard
		Scanner movieFile = null; //movie file
		Scanner showFile = null; //show file
		
		// ***** create movieFile Scanner
		try {
			movieFile = new Scanner(new File(args[0]));
		}
		//if there is no parameter for showings file
		catch (ArrayIndexOutOfBoundsException exc) { 
			//if there is nothing at args[0], there weren't any parameters
			System.out.println("No arguments were passed in for the movies " +
					"and showings files.");
			System.exit(0); 
		}
		//if the file doesn't exist or it fails to open
		catch (FileNotFoundException exc) {
			System.out.println("First file from parameters (movie list) " +
					"could not be opened.");
			System.exit(0);
		}
		// ***** end
		
		// ***** create showFile Scanner
		try {
			showFile = new Scanner(new File(args[1]));
		}
		//if there is no parameter for showings file
		catch (ArrayIndexOutOfBoundsException exc) {
			System.out.println("No parameter entered for showings list " +
					"file name.");
			System.exit(0);
		}
		//if the file doesn't exist or it fails to open
		catch (FileNotFoundException exc) {
			System.out.println("Second file from parameters (showings list) " +
					"could not be opened.");
			System.exit(0);
		}
		// ***** end
		
		database.storeMovies(movieFile); //store movies
		database.storeShowings(showFile); //store showings
		
		while(true) {
			menu(console); //call the menu
		}
	}
	
	
	/*
	 * Method: menu
	 * Purpose: Print the menu and handle the user's selections
	 * Parameters:
	 * 		Scanner console:	scanner of user input
	 * Returns: void
	 */
	
	public static void menu(Scanner console) {
		int selected = 0; //stores menu selection
		
		final int PRINT_ALL = 1; //const for print
		final int SEARCH = 2; //const for search
		final int ADD_MOVIE = 3; //cost for adding movie
		final int ADD_SHOW = 4; //const for adding showing
		final int VALIDATE = 5; //const for validate db
		final int EXIT = 0; //const for exit
		
		//Print the menu'
		System.out.println("\n------------------------------------");
		System.out.println("Enter one of the following commands:");
		System.out.println("------------------------------------");
		System.out.println(PRINT_ALL + ") Print movies and showings");
		System.out.println(SEARCH + ") Search for a movie");
		System.out.println(ADD_MOVIE + ") Add movie to database");
		System.out.println(ADD_SHOW + ") Add showing to database");
		System.out.println(VALIDATE + ") Validate database");
		System.out.println(EXIT + ") Exit");
		System.out.print("\nInput >> ");
		
		if(console.hasNextInt()) { //check for int input
			selected = console.nextInt();
			console.nextLine(); //Moves the pointer to a new line
			
			switch (selected) {
			    case PRINT_ALL: //print everything
			    	System.out.println(); //space
			    	database.printAll();
			    	System.out.println("\nPress ENTER to continue..");
			    	console.nextLine(); //wait for user..
			    	break;
			    case SEARCH: //search for movie
			    	System.out.println(); //space
				    System.out.print("What movie are you looking for? >> ");
				    database.searchMovies(console.nextLine());
				    System.out.println("\nPress ENTER to continue..");
			    	console.nextLine(); //wait for user..
				    break;
			    case ADD_MOVIE:
			    	System.out.println("Please input movie data in the " +
			    			"following form:");
			    	System.out.println("unique ID/title/time in " +
			    			"mins/rating");
			    	System.out.print("\nInput >> ");
			    	database.usrAddMovie(console);
			    	System.out.println("Movie added successfully..");
			    	System.out.println("\nPress ENTER to continue..");
			    	console.nextLine(); //wait for user..
			    	break;
			    case ADD_SHOW:
			    	System.out.println("Please input showing data in the " +
			    			"following form:");
			    	System.out.println("unique ID/yyyymmdd/hhmm ");
			    	System.out.print("\nInput >> ");
			    	database.usrAddShow(console);
			    	System.out.println("\nPress ENTER to continue..");
			    	console.nextLine(); //wait for user..
			    	break;
			    case VALIDATE: //validate db
			    	System.out.println(); //space
			    	System.out.println("Input date in the form yyyymmdd");
					System.out.println("Input >>");
			    	database.validateDB(console.nextLine());
			    	System.out.println("\nPress ENTER to continue..");
			    	console.nextLine(); //wait for user..
			    	break;
			    case EXIT: //exit the program
			    	System.out.println("\nExiting the program..");
			    	System.exit(0);
			    	break;
			    default: //if any other cases arise, this is incorrect input
			    	System.out.println("Invalid Input");
		    }
		}
		else if(console.hasNext()) {
			System.out.println("Invalid Input");
		}
	}
}